﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using YLPublicService.TransferModel;
using System.Collections.ObjectModel;

using winFrm = System.Windows.Forms;
namespace YLServerInterface.UserControls.TemplateBuilder
{
    /// <summary>
    /// Interaction logic for TemplateItemShower.xaml
    /// </summary>
    public partial class TemplateItemShower : UserControl
    {
        public TemplateItemShower()
        {
            InitializeComponent();

        }
        public ObservableCollection<T>
            ToObserable<T>(IEnumerable<T> src)
        {
            ObservableCollection<T> newCollection = new ObservableCollection<T>(src);
            return newCollection;
        }

        public void BindingDataContext()
        {
            var templateItems = DataContext as TemplateItem;
            if (templateItems != null && templateItems.mID != null)
            {
                txtTemplateName.Text = templateItems.mTemplateDetial.mName;
                txtTemplatePrice.Text = templateItems.mTemplateDetial.mPrice;
                ckbTemplateIsEnable.IsChecked = templateItems.mEnable.ToUpper() == "1" ? true : false;
                txtAttachmentDir.Text = templateItems.mAttachmentsDir;
                lstRequireValues.DataContext =
                    ToObserable(templateItems.mTemplateDetial.mRequireValues.mItem);
                lstRequireAttachments.DataContext =
                    ToObserable(templateItems.mTemplateDetial.mRequireAttchments.mItem);
            }
        }

        private void btnUploadInfo_Click(object sender, RoutedEventArgs e)
        {
            var templateItem = DataContext as TemplateItem;
            var client = YLServiceCLient.YLAdminClient.AdminServiceClient.GetServiceClient();
            bool ret;
            templateItem.mTemplateDetial.mRequireValues.mItem =
                (lstRequireValues.DataContext as ObservableCollection<TemplateItem.TemplateDetial.RequireValues.Item>)
                .ToList();
            templateItem.mTemplateDetial.mRequireAttchments.mItem =
                (lstRequireAttachments.DataContext as ObservableCollection<TemplateItem.TemplateDetial.RequireAttchments.Item>)
                .ToList();
            if (templateItem.mID != null || templateItem.mID != "")
            {//update
                ret = client.UpdateTemplate(templateItem);
            }
            else
            {//insert
                ret = client.AddTemplate(templateItem);
            }
            MessageBox.Show("操作结果：" + (ret == true ? "成功" : "失败"));
        }

        private void btnAddRequireValue_Click(object sender, RoutedEventArgs e)
        {
            var emptyItem = new TemplateItem.TemplateDetial.RequireValues.Item();
            var rvs = lstRequireValues.DataContext as ObservableCollection<TemplateItem.TemplateDetial.RequireValues.Item>;
            rvs.Add(emptyItem);
        }

        private void attachMenuItemDelete_Click(object sender, RoutedEventArgs e)
        {
            var rvs = lstRequireAttachments.DataContext as ObservableCollection<TemplateItem.TemplateDetial.RequireAttchments.Item>;
            rvs.Remove(lstRequireAttachments.SelectedItem as TemplateItem.TemplateDetial.RequireAttchments.Item);
        }
        private void requireValueMenuItemDelete_Click(object sender, RoutedEventArgs e)
        {
            var rvs = lstRequireValues.DataContext as ObservableCollection<TemplateItem.TemplateDetial.RequireValues.Item>;
            rvs.Remove(lstRequireValues.SelectedItem as TemplateItem.TemplateDetial.RequireValues.Item);
        }

        private void btnAddAttachment_Click(object sender, RoutedEventArgs e)
        {
            var emptyItem = new TemplateItem.TemplateDetial.RequireAttchments.Item();
            var rvs = lstRequireAttachments.DataContext as ObservableCollection<TemplateItem.TemplateDetial.RequireAttchments.Item>;
            rvs.Add(emptyItem);

        }

        private void btnFilesUpload_Click(object sender, RoutedEventArgs e)
        {
            winFrm.OpenFileDialog dialog = new winFrm.OpenFileDialog();
            dialog.Multiselect = true;
            var ret = dialog.ShowDialog();
            if (ret == winFrm.DialogResult.OK)
            {
                var files = dialog.FileNames;
                FTP.ClientAdapter client = new FTP.ClientAdapter(false);
                var templateItem = DataContext as TemplateItem;
                var result = client.UploadFiles(templateItem.mAttachmentsDir, files);
                MessageBox.Show("上传模板必要文件：" + result.ToString());
            }
        }
    }
}
